package teaching.io;

import java.io.EOFException;
import java.io.File;
import java.io.IOException;
import java.io.RandomAccessFile;

public class EmployeeFileOperator {
  private static final int NUMBER_RECORD = 100;
  private File file;

  public EmployeeFileOperator() {
    super();
    File file = new File("d:/employee.dat");
    this.file = file;
  }

  public EmployeeFileOperator(File file) {
    super();
    this.file = file;
  }

  private void addRecord(RandomAccessEmployeeRecord employee) {
    RandomAccessFile randomAccessFile = null;
    try {
      randomAccessFile = new RandomAccessFile(file, "rw");
      int employeeId = employee.getId();

      if (employeeId > 0 && employeeId <= NUMBER_RECORD) {
        randomAccessFile.seek((employeeId - 1) * RandomAccessEmployeeRecord.SIZE);
        employee.write(randomAccessFile);
        System.out.println("write ok.");
      } else {
        System.out.println("error");
      }
    } catch (IOException ex) {
      ex.printStackTrace();
    } finally {
      closeFile(randomAccessFile);
    }
  }

  public RandomAccessEmployeeRecord getFirstRecord() {
    return null;
  }

  public RandomAccessEmployeeRecord getLastRecord() {
    return null;
  }

  private void closeFile(RandomAccessFile randomAccessFile) {
    try {
      randomAccessFile.close();
    } catch (IOException ex) {
      ex.printStackTrace();
    }
  }

  private RandomAccessEmployeeRecord readRecord() {
    RandomAccessFile randomAccessFile = null;
    RandomAccessEmployeeRecord employeeRecord = null;

    try {
      randomAccessFile = new RandomAccessFile(file, "r");
      employeeRecord = new RandomAccessEmployeeRecord();
      do {
        employeeRecord.read(randomAccessFile);
      } while (employeeRecord.getId() == 0);

      System.out.println(employeeRecord);
    } catch (EOFException ex) {
      ex.printStackTrace();
      System.out.println("�]�и���ӛ䛡�");
    } catch (IOException ex) {
      ex.printStackTrace();
    } finally {
      closeFile(randomAccessFile);
    }
    return employeeRecord;
  }

  private void readRecord(int id) {
    if (id > 0 && id <= NUMBER_RECORD) {
      System.out.println("������");
    }
    RandomAccessEmployeeRecord employeeRecord = new RandomAccessEmployeeRecord();
    File file = new File("d:/emp.dat");
    RandomAccessFile randomAccessFile = null;

    try {
      randomAccessFile = new RandomAccessFile(file, "r");
      randomAccessFile.seek((id - 1) * RandomAccessEmployeeRecord.SIZE);
      System.out.println((id - 1) * RandomAccessEmployeeRecord.SIZE);
      employeeRecord.read(randomAccessFile);
      System.out.println(employeeRecord);
    } catch (IOException e) {
      e.printStackTrace();
    } finally {
      closeFile(randomAccessFile);
    }
  }

  public static void main(String[] args) {

    // WriteRandomFile wrf = new WriteRandomFile();
    // wrf.addRecord(new RandomAccessEmployeeRecord(3, "����", 2500.0d));
    // wrf.addRecord(new RandomAccessEmployeeRecord(2, "����", 3500.0d));

    // WriteRandomFile wrf = new WriteRandomFile();
    // wrf.readRecord(1);
    // wrf.readRecord(2);

    EmployeeFileOperator wrf = new EmployeeFileOperator();
    wrf.readRecord();

  }
}
